/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package redis

import (
	"github.com/go-redis/redis"
	"gitee.com/tugoer/orivil/config"
)

var env = &struct {
	Addr string
	Password string
}{
	Addr:     "192.168.163.130:6379",
	Password: "ksn712369", // no password set
}

func NewDB(db int) *redis.Client {
	err := config.NewJsonParser("configs").ReadOrCreate("redis", &env)
	if err != nil {
		panic(err)
	}
	client := redis.NewClient(&redis.Options{
		Addr:     env.Addr,
		Password: env.Password, // no password set
		DB:       db,  // use default DB
	})
	_, err = client.Ping().Result()
	if err != nil {
		panic(err)
	}
	return client
}